package com.cls.common.utils;

import com.cls.business.entity.SysSms;
import com.cls.business.service.ISysSmsService;
import com.cls.common.enums.EmailTextEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class EmailSenderUtil {


    @Autowired
    JavaMailSender javaMailSender;
    @Autowired
    ISysSmsService smsService;

    @Value("${spring.mail.username}")
    private String emailUsername;


    /**
     * 发送邮件
     *
     * @param subject     邮件主题
     * @param textContent 邮件内容
     * @param receiveMail 收件人邮箱地址
     * @author Carey
     * @description
     * @date 2020/12/22 2:31 下午
     */
    @Async
    public void sendEmail(String subject, String textContent, String receiveMail) {
        // 简单邮件对象
        SimpleMailMessage msg = new SimpleMailMessage();
        Date now = new Date();
        msg.setSubject(subject);
        msg.setText(textContent);
        // 发件人邮箱
        msg.setFrom(emailUsername);
        msg.setSentDate(now);
        //收件人邮箱
        msg.setTo(receiveMail);
        //发送邮件

        SysSms entity = new SysSms();
        entity.setContentJson(msg.getText());
        entity.setReceiveMobile(receiveMail);
        entity.setSendTime(msg.getSentDate());

        try {
            javaMailSender.send(msg);
            entity.setSendType(true);
        } catch (MailException e) {
            System.out.println("发送邮件异常");
            entity.setSendType(false);
        } finally {
            smsService.createSysSms(entity);
        }
    }

    /**
     * 发送年度课题评审提示邮件
     *
     * @param paramArray  接收参数 [0]：年份 [1]:用户名 [2]:密码
     * @param receiveMail 收件人邮箱地址
     * @return a
     * @author Carey
     * @description
     * @date 2020/12/22 2:31 下午
     */
    public void sendAnnualPromptEmail(String receiveMail, List<String> paramArray) {
        if (paramArray.size() != 3) {
            return;
        }
        String text = String.format(EmailTextEnum.ANNUAL_PROMPT_TEXT.getText(), paramArray.get(0), paramArray.get(1), paramArray.get(2));
        this.sendEmail(EmailTextEnum.ANNUAL_PROMPT.getText(), text, receiveMail);
    }

    /**
     * 发送年度课题评审提示邮件
     *
     * @param paramArray  接收参数 [0]：年份 [1]：期次 [2]:用户名 [3]:密码
     * @param receiveMail 收件人邮箱地址
     * @return a
     * @author Carey
     * @description
     * @date 2020/12/22 2:31 下午
     */
    public void sendEntrustPromptEmail(String receiveMail, List<String> paramArray) {
        if (paramArray.size() != 4) {
            return;
        }
        String text = String.format(EmailTextEnum.ENTRUST_PROMPT_TEXT.getText(), paramArray.get(0), paramArray.get(1), paramArray.get(2), paramArray.get(3));
        this.sendEmail(EmailTextEnum.ENTRUST_PROMPT.getText(), text, receiveMail);
    }
}
